﻿#include "parambasicwidget.h"

QParamBasicWidget::QParamBasicWidget(QWidget *parent) : QWidget(parent)
{
    //故障信息
    _errCodeInfo[0x00]="无故障";
    _errCodeInfo[0x01]="软件故障";
    _errCodeInfo[0x02]="驱动器故障";
    _errCodeInfo[0x03]="过流故障";
    _errCodeInfo[0x04]="超速故障";

    _bStartTest = false;
    _i32FrameCnt = 0;
    _bStartSaveData = false;
    _i32SaveFrameCnt = 0;

    _lfSpeedForwardGain = 0.0;

    _i32SpeedForwardEnable = 0;


    _lfACurrentdata = 0.0;
    _lfBCurrentdata = 0.0;
    _lfCurrentdata = 0.0;

    _lfMotorTemplate = 0.0;
    _lfIPMTemplate = 0.0;

    _lfQSetdata = 0.0;
    _lfQResponsedata = 0.0;

    _lfSpeedSetdata = 0.0;
    _lfSpeedRespdata = 0.0;

    _lfPosSetdata = 0.0;
    _lfPosRespdata = 0.0;

}
/**
* @brief
*
**/
void QParamBasicWidget::parsePIDParam(st_pid_frame& framedata)
{
    fourchar l_data;
    l_data.data[0] = framedata.data[3];
    l_data.data[1] = framedata.data[2];
    l_data.data[2] = framedata.data[1];
    l_data.data[3] = framedata.data[0];

    switch (framedata.cmdid)
    {
        case 0x11:
            m_st_pos_pid.lfKP =  l_data.ui32data;
            break;
        case 0x12:
            m_st_pos_pid.lfKI =  l_data.ui32data;
            break;
        case 0x13:
            m_st_pos_pid.lfKD =  l_data.ui32data;
            break;
        case 0x14:
            _lfInputSaturMax = l_data.ui32data;
            break;
        case 0x15:
            _lfOutputSaturMax = l_data.ui32data;
            break;
        case 0x16:
            _lfPosForwardGain = l_data.ui32data;
            break;
        case 0x21:
            m_st_speed_pid.lfKP = l_data.ui32data;
            break;
        case 0x22:
            m_st_speed_pid.lfKI = l_data.ui32data;
            break;
        case 0x23:
            _lfSpeedForwardGain = l_data.ui32data;
            break;
        case 0x24:
            _i32SpeedForwardEnable = l_data.ui32data;
            break;
        case 0x31:
            m_st_current_pid.lfKP = l_data.ui32data;
            break;
        case 0x32:
            m_st_current_pid.lfKI = l_data.ui32data;
            break;
        case 0x33:
            m_st_current_pid.lfKD = l_data.ui32data;
            break;
    }
}
/**
* @brief
*
**/
void QParamBasicWidget::parseParam(st_loopbackdata& framedata)
{
    //Q相电流 反馈值
    _lfQResponsedata = framedata.i16QRespdata /10.0;
    _lfQSetdata = framedata.i16QSetdata/10.0;

    //AB相电流
    _lfACurrentdata = framedata.i16CurrentA/10.0;
    _lfBCurrentdata = framedata.i16CurrentB/10.0;
    _lfCurrentdata = framedata.i16Current/10.0;

    _lfSpeedSetdata = framedata.i16SpeedSetdata;
    _lfSpeedRespdata = framedata.i16SpeedRespdata;

    _lfPosSetdata = framedata.i32PosSetdata;
    _lfPosRespdata = framedata.i32PosRespdata;

    _lfMotorTemplate = framedata.i16MotorTemplate/10.0;
    _lfIPMTemplate = framedata.i16IPMTemplate;

}

/**
* @brief
*
**/
QString QParamBasicWidget::getFrameStatus(st_loopbackdata& framedata)
{
    QString sInfo = QString("母线电流:%1A\n").arg(_lfCurrentdata);
    sInfo = sInfo + QString("电机温度:%1°C\n").arg(_lfMotorTemplate);
    sInfo = sInfo + QString("IPM温度:%1°C\n").arg(_lfIPMTemplate);
    sInfo = sInfo + QString("故障状态:%1\n").arg(_errCodeInfo[framedata.u8Errcode]);

    sInfo = sInfo + QString("\n系统状态:\n");

    un_status l_un_status;
    l_un_status.data = framedata.u8Status;
    if(l_un_status.statuscode.enablestatus == 0x0)
    {
        sInfo = sInfo + QString("使能输入线:开启\n");
    }
    if(l_un_status.statuscode.enablestatus == 0x0)
    {
        sInfo = sInfo + QString("使能输入线:关闭\n");
    }

    if(l_un_status.statuscode.CWEnablestatus == 0x0)
    {
        sInfo = sInfo + QString("CW限位状态:未达到\n");
    }
    if(l_un_status.statuscode.enablestatus == 0x0)
    {
        sInfo = sInfo + QString("CW限位状态:达到\n");
    }
    if(l_un_status.statuscode.CCWEnableStatus == 0x0)
    {
        sInfo = sInfo + QString("CCW限位状态:未达到\n");
    }
    if(l_un_status.statuscode.CCWEnableStatus == 0x0)
    {
        sInfo = sInfo + QString("CCW限位状态:达到\n");
    }

    return sInfo;

}



/**
* @brief
*
**/
void QParamBasicWidget::saveDataToFile()
{
    if(_bStartSaveData == false)
    {
        return;
    }

    _i32SaveFrameCnt++;

    int l_time = _i32SaveFrameCnt*10;

    QString sInfo;
    sInfo = QString("%1,%2,%3,%4,%5,%6,%7,%8\r").arg(l_time).arg(_lfACurrentdata).arg(_lfBCurrentdata).\
            arg(_lfPosSetdata).arg(_lfPosRespdata).arg(_lfSpeedSetdata).arg(_lfSpeedRespdata,0,'f',6).arg(_lfQSetdata,0,'f',6)\
            .arg(_lfQResponsedata,0,'f',6);

    m_sSaveDataInfo = m_sSaveDataInfo + sInfo;
    if(m_sSaveDataInfo.size() > 1000)
    {
        writeDataToFile(m_sSaveDataInfo);
        m_sSaveDataInfo = "";
    }

}

void QParamBasicWidget::writeDataToFile(QString str)
{
    if(m_savefile.fileName().isEmpty())
    {
        return;
    }

    if(!m_savefile.isOpen())
    {
        m_savefile.open(QFile::ReadWrite|QFile::Append);
    }

    m_savefile.write(str.toLocal8Bit());
}

